package com.lhn;

/**
 * @author lhn
 * @date 2024-08-21 13:31
 * 代理模式
 */

public class ProxyPattern {
    public static void main(String[] args) {
        new RealSubjectProxy().doWork();
    }
}

interface Subject {
    void doWork();
}

class RealSubject implements Subject {

    @Override
    public void doWork() {
        System.out.println("Real Subject doWork ...");
    }
}

class RealSubjectProxy implements Subject {
    private RealSubject realSubject;
    public RealSubjectProxy() {
        try {
            //通过类加载器获取类对象
            this.realSubject = (RealSubject) this.getClass().getClassLoader().loadClass("com.lhn.RealSubject").newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void connect() {
        System.out.println("connect ...");
    }
    public void log() {
        System.out.println("log ...");
    }
    @Override
    public void doWork() {
        connect();
        System.out.println("Real Subject Proxy doWork ...");
        realSubject.doWork();
    }
}